package com.fan.controller;


import com.fan.entity.Article;
import com.fan.utils.AjaxResult;
import com.fan.service.Impl.ArticleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class ArticleController {

    @Autowired
    ArticleServiceImpl articleService;

    @PostMapping("/addArticle")
    public AjaxResult addArticle( Article article) {

        System.out.println(article.toString());

        articleService.addArticle(article);

        return AjaxResult.success("操作成功");
    }

    @GetMapping("/deleteArticle")
    public AjaxResult deleteArticle() {
        int a  = articleService.deleteArticle(1);
        System.out.println(a);
        return AjaxResult.success("操作成功");
    }

    @GetMapping("/getAll")
    public AjaxResult getAll() {
        List<Article> list = articleService.queryAllArticle();
//        System.out.println(list);
        return AjaxResult.success("操作成功",list);
    }

    @GetMapping("/getAllByTitle")
    public AjaxResult getAllByTitle(String title) {

        HashMap<String,Object> map = new HashMap<String,Object>();
        map.put("title",title);

        List<Article> list = articleService.queryAllArticleByTitle(map);
        return AjaxResult.success("操作成功",list);
    }

    @GetMapping("/getArticleById")
    public AjaxResult getArticleById(int articleId) {
        Article article = articleService.getArticleById(articleId);
        return AjaxResult.success("操作成功",article);
    }

    @GetMapping("/getHeatArticle")
    public AjaxResult getHeatArticle(){
        List<Article> list = articleService.queryAllArticle();
        //按点赞排序
        list = list.stream().sorted(Comparator.comparing(Article::getLikeCount)
                        .reversed())
                .collect(Collectors.toList());
        return AjaxResult.success("操作成功",list.subList(0,5));
    }

    @GetMapping("/addViews")
    public AjaxResult addViews(int articleId){
        int i = articleService.addViews(articleId);
        return AjaxResult.success("点击量+1",i);
    }

    @GetMapping("/getCommentCount")
    public AjaxResult getCommentCount(int articleId){
        int i = articleService.getCommentCount(articleId);
        return AjaxResult.success("查询评论数成功",i);
    }

    @GetMapping("/getLikeCount")
    public AjaxResult getLikeCount(int articleId){
        int i = articleService.getLikeCount(articleId);
        return AjaxResult.success("查询点赞数成功",i);
    }







}
